package com.youlu.campus.admin.banner.controller;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.admin.banner.dto.BannerConfigCreateRequest;
import com.youlu.campus.admin.banner.dto.BannerConfigQueryRequest;
import com.youlu.campus.admin.banner.dto.BannerConfigUpdateRequest;
import com.youlu.campus.admin.banner.entity.BannerConfig;
import com.youlu.campus.admin.banner.service.BannerConfigService;
import com.youlu.campus.admin.utils.AuthUtils;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * Banner配置控制层
 * 描述：Banner管理
 */
@Slf4j
@RestController
public class BannerConfigController {
    @Autowired
    private BannerConfigService bannerConfigService;

    /**
     * Banner列表
     * @param request Banner查询请求类
     * @return
     */
    @PostMapping("/banner-config/list")
    public QResult list(@NotNull @RequestBody BannerConfigQueryRequest request) {
        log.info(":>>> 开始查询Banner:{}", JSON.toJSONString(request));
        BannerConfig data = new BannerConfig();
        BeanUtils.copyProperties(request, data);
        Page<BannerConfig> datas = bannerConfigService.list(data, request.getPage(), request.getPageSize());
        log.info(":>>> 查询Banner结果为:{}", datas);
        return QResult.success(datas);
    }


    /**
     * 创建Banner
     * @param request Banner创建请求类
     * @return
     */
    @PostMapping("/banner-config/create")
    public QResult create(@NotNull @RequestBody BannerConfigCreateRequest request) {
        log.info(":>>> 开始创建Banner:{}", JSON.toJSONString(request));
        BannerConfig data = new BannerConfig();
        BeanUtils.copyProperties(request, data);
        data.setTenantId(AuthUtils.getComId());
        boolean result = bannerConfigService.create(data);
        log.info(":>>> 创建Banner结果为:{}", request);
        if (result) {
            return QResult.success(result);
        }
        return QResult.fail("创建Banner错误");
    }

    /**
     * 删除Banner
     * @param id BannerID
     * @return
     */
    @GetMapping("/banner-config/delete")
    public QResult delete(@NotNull @RequestParam("id") String id) {
        log.info(":>>> 开始删除 Banner id:{}", id);
        boolean result = bannerConfigService.delete(id);
        log.info(":>>> 删除Banner结果为:{}", result);
        if (result) {
            return QResult.success(result);
        }
        return QResult.fail("删除Banner错误");
    }

    /**
     * 修改Banner
     * @param updateRequest Banner修改请求类
     * @return 请求成功时返回Boolean对象
     */
    @PostMapping("/banner-config/update")
    public QResult update(@NotNull @RequestBody BannerConfigUpdateRequest updateRequest) {
        log.info(":>>> 开始修改 Banner:{}", JSON.toJSONString(updateRequest));
        BannerConfig data = new BannerConfig();
        BeanUtils.copyProperties(updateRequest, data);
        boolean result = bannerConfigService.update(data);
        log.info(":>>> 修改Banner结果为:{}", result);
        if (result) {
            return QResult.success(result);
        }
        return QResult.fail("修改Banner错误");
    }

    /**
     * 修改Banner状态
     * @param updateRequest Banner修改请求类
     * @return
     */
    @PostMapping("/banner-config/status/update")
    public QResult updateStatus(@RequestBody BannerConfigUpdateRequest updateRequest) {
        log.info(":>>> 开始修改 Banner:{}", JSON.toJSONString(updateRequest));
        boolean result = bannerConfigService.updateStatus(updateRequest);
        log.info(":>>> 修改Banner结果为:{}", result);
        if (result) {
            return QResult.success(result);
        }
        return QResult.fail("修改Banner状态错误");
    }

    /**
     * 查询单个Banner
     * @param id BannerID
     * @return
     */
    @GetMapping("/banner-config/find-one")
    public QResult findOne(@NotNull @RequestParam("id") String id) {
        return QResult.success(bannerConfigService.findOne(id));
    }
}